package com.crawler.mybatis;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.fasterxml.jackson.annotation.JsonIgnore;  
  
/**
 * 分页对象
 * @author rubekid
 *
 * 2017年10月20日 下午2:04:07
 */
public class Page<T> {  
  
    public static final int DEFAULT_PAGE_SIZE = 10;  
  
    protected int pageNo = 1; // 当前页, 默认为第1页  
    protected int pageSize = DEFAULT_PAGE_SIZE; // 每页记录数  
    protected long totalCount = -1; // 总记录数, 默认为-1, 表示需要查询  
    protected int totalPages = -1; // 总页数, 默认为-1, 表示需要计算  
	protected String orderBy = ""; // 排序
  
    protected List<T> items; // 当前页记录List形式  
      
    public Map<String, Object> params = new HashMap<String, Object>();//设置页面传递的查询参数  
  
	public Page() {
		setPageSize(DEFAULT_PAGE_SIZE);
	}
	
	public Page(Integer pageSize, Integer pageNo){
		if(pageSize > 100){
			throw new RuntimeException("分页大小不能大于100");
		}
		setPageSize(pageSize);
		setPageNo(pageNo);
	}
    
	@JsonIgnore
    public Map<String, Object> getParams() {  
        return params;  
    }
  
    public void setParams(Map<String, Object> params) {  
        this.params = params;  
    }  
  
    public int getPageNo() {  
        return pageNo;  
    }  
  
    public void setPageNo(int pageNo) {  
        this.pageNo = pageNo;  
    }  
  
    public int getPageSize() {  
        return pageSize;  
    }  
  
    public void setPageSize(int pageSize) {  
        this.pageSize = pageSize;  
        computeTotalPage();  
    }  
  
    public long getTotalCount() {  
        return totalCount;  
    }  
  
    public int getTotalPages() {  
        return totalPages;  
    }  
  
    public void setTotalCount(long totalCount) {  
        this.totalCount = totalCount;  
        computeTotalPage();  
    }  
  
    protected void computeTotalPage() {  
        if (getPageSize() > 0 && getTotalCount() > -1) {  
            this.totalPages = (int) (getTotalCount() % getPageSize() == 0 ? getTotalCount() / getPageSize() : getTotalCount() / getPageSize() + 1);  
        }  
    }  
    
    @JsonIgnore
    public String getOrderBy() {
	    if(orderBy == null || "".equals(orderBy.trim())){
	        return null;
        }
        // SQL过滤，防止注入
        String reg = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|"
                + "(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";
        Pattern sqlPattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
        if (sqlPattern.matcher(orderBy).find()) {
            return "";
        }
        return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
  
    public List<T> getItems() {  
        return items;  
    }  
  
    public void setItems(List<T> items) {  
        this.items = items;  
    }  
  


	@Override  
    public String toString() {  
        StringBuilder builder = new StringBuilder().append("Page [pageNo=").append(pageNo).append(", pageSize=").append(pageSize)  
                .append(", totalRecord=").append(totalCount < 0 ? "null" : totalCount).append(", totalPage=")  
                .append(totalPages < 0 ? "null" : totalPages).append(", curPageObjects=").append(items == null ? "null" : items.size()).append("]");  
        return builder.toString();  
    }  
  
}  